package com.ydl.hostmonitor.controller;

import com.ydl.hostmonitor.service.SystemDetailService;
import com.ydl.hostmonitor.service.SystemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/system")
public class SystemInfoController {

    @Autowired
    private SystemInfoService systemInfoService;

    @Autowired
    private SystemDetailService systemDetailService;

    // 原有接口保持不变...
    @GetMapping("/cpu")
    public Map<String, Object> getCpuInfo() {
        return systemInfoService.getCpuInfo();
    }

    @GetMapping("/memory")
    public Map<String, Object> getMemoryInfo() {
        return systemInfoService.getMemoryInfo();
    }

    @GetMapping("/disk")
    public Map<String, Object> getDiskInfo() {
        return systemInfoService.getDiskInfo();
    }

    @GetMapping("/overview")
    public Map<String, Object> getSystemOverview() {
        return systemInfoService.getSystemOverview();
    }

    // 新增接口
    @GetMapping("/jvm")
    public Map<String, String> getJvmInfo() {
        return systemDetailService.getJvmInfo();
    }

    @GetMapping("/fonts")
    public List<String> getAvailableFonts() {
        return systemDetailService.getAvailableFonts();
    }

    // 目录打包接口（移除参数，使用固定目录）
    @GetMapping("/zip")
    public ResponseEntity<byte[]> downloadZip() {
        try {
            byte[] zipData = systemDetailService.zipDirectory();  // 调用无参方法

            HttpHeaders headers = new HttpHeaders();
            // 固定目录为/tmp，ZIP文件名包含目录标识
            String fileName = "tmp-directory-" + System.currentTimeMillis() + ".zip";
            headers.setContentDispositionFormData("attachment", fileName);
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

            return new ResponseEntity<>(zipData, headers, HttpStatus.OK);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }
}